<?php
/**
 * Created by PhpStorm.
 * User: yafex
 * Date: 2020/11/9
 * Time: 14:16
 */

class serv_rep_wish extends L {

    /**
     * 描述 : 买家视角收藏产品
     * 作者 : Zoro.Zhu
     */
    public static function addProduct($params){
        $url = "https://www.wish.com/api/user/wishlist/add-product";
        $url2 = "https://www.wish.com/api/analytics/CLICK_WEB_ADD_TO_WISHLIST";
        $post = [
            'product_ids[]' => $params['productId'],
            'securedtouch_token' => $params['token'],
        ];
        $header = [
            'cookie:' . $params['cookie'],
            'x-xsrftoken:' . $params['xToken'],
        ];
        $res = of_base_com_net::request($url, ['post' => $post, 'header' => $header]);
        unset($post['product_ids[]']);
        of_base_com_net::request($url2, ['post' => $post, 'header' => $header], true);
        if($res['state']){
            $response = json_decode($res['response'], true);
            if($response['code'] === 0){
                return true;
            } else {
                return $response['msg'];
            }
        }
        if($res['errstr']){
            $errstr = json_decode($res['errstr'], true);
            return !empty($errstr['msg']) ? $errstr['msg'] : '操作错误';
        }
        return '网络异常';
    }

    /**
     * 描述 : 添加购物车
     * 作者 : Zoro.Zhu
     */
    public static function addToCart($params){
        $url = "https://www.wish.com/api/cart/update";
        $post = [
            'product_id' => $params['productId'],
            'variation_id' => $params['variation_id'],
            'quantity' => 1,
            'add_to_cart' => 'true',
            'shipping_option_id' => 'standard',
            'product_source' => 'search',
            'securedtouch_token' => $params['token'],
        ];
        $header = [
            'cookie:' . $params['cookie'],
            'x-xsrftoken:' . $params['xToken'],
        ];
        $res = of_base_com_net::request($url, ['post' => $post, 'header' => $header]);
//        p($res);
        $url = "https://www.wish.com/api/analytics/IMPRESSION_IMPACT_ADD_TO_CART";
        $post = [
            'productID' => $params['productId'],
            'securedtouch_token' => $params['token'],
        ];
        $url2 = "https://www.wish.com/api/analytics/COZY__CLICK_PRODUCT_BUY_BUTTON";
        $url3 = "https://www.wish.com/api/analytics/CLICK_WEB_PRODUCT_PAGE_BUY_BUTTON";
        $post2 = [
            'current_id' => $params['productId'],
            'securedtouch_token' => $params['token'],
            'product_source' => 'search',
        ];
        of_base_com_net::request($url, ['post' => $post, 'header' => $header], true);
        of_base_com_net::request($url2, ['post' => $post2, 'header' => $header], true);
        of_base_com_net::request($url3, ['post' => $post2, 'header' => $header], true);
        if($res['state']){
            $response = json_decode($res['response'], true);
            if($response['code'] === 0){
                return true;
            }
        }
        return null;
    }

    /**
     * 描述 : 浏览产品页面
     * 作者 : Zoro.Zhu
     */
    public static function lookProduct($params, $addCart = false){
        $url = "https://www.wish.com/product/{$params['productId']}?share=web";
        $header = [
            'cookie:' . $params['cookie'],
            'x-xsrftoken:' . $params['xToken'],
        ];
//        $res = of_base_com_net::request($url, ['type' => 'get', 'header' => $header], empty($addCart));
        $res = core_base::curl($url, null,'get', $header);
        if($addCart === true){
            $json = substr($res, strpos($res, 'window.__PRELOADED_STATE__ = '));
            $json = substr($json, 29, strpos($json, '</script>'));
            $json = substr($json, 0, strpos($json, '</script>'));
            $json = trim($json);
            $data = json_decode($json, true);
            if(!empty($data)){
                $variation = $data['data']['product'][$params['productId']];
            }
        }
//        $url = "https://www.wish.com/api/related-feed/get";
//        $post = [
//            'contest_id' => $params['productId'],
//            'offset' => 0,
//            'feed_mode' => 'similar',
//            'count' => 24,
//        ];
//        of_base_com_net::request($url, ['post' => $post, 'header' => $header], true);
//        $post = [
//            'contest_id' => $params['productId'],
//            'offset' => 0,
//            'feed_mode' => 'express',
//            'count' => 30,
//        ];
//        of_base_com_net::request($url, ['post' => $post, 'header' => $header], true);
//        //这些数据暂时没啥用，就访问一下
//        $url = "https://www.wish.com/api/mobile/get-product-ugc-ratings";
//        $post = [
//            'product_id' => $params['productId'],
//            'start' => 0,
//        ];
//        of_base_com_net::request($url, ['post' => $post, 'header' => $header], true);
//        //更新一下用户状态
//        $url = "https://www.wish.com/api/web/user/status";
//        $post = [
//            'securedtouch_token' => $params['token'],
//        ];
//        of_base_com_net::request($url, ['post' => $post, 'header' => $header], true);
        return !empty($variation) ? $variation : [];
    }

    /**
     * 描述 : 获取产品数据
     * 作者 : Zoro.Zhu
     */
    public static function getProductInfo($params){
        $url = "https://www.wish.com/api/product/get";
        $header = [
            'cookie:' . $params['cookie'],
            'x-xsrftoken:' . $params['xToken'],
        ];
        $post = [
            'cid' => $params['productId'],
            'request_sizing_chart_info' => 'true',
            'do_not_track' => 'false',
        ];
        $res = core_base::curl($url, $post, 'post', $header);
        $response = json_decode($res, true);
        if(!empty($response['msg'])) {
            return false;
        }
        if(!empty($response['data']['contest'])) {
            return $response['data']['contest'];
        }
        return [];
    }

    /**
     * 描述 : 获取产品信息新的接口
     * 作者 : Zoro.Zhu
    **/
    public static function getProductInfoNew($params){
        $url = "https://www.wish.com/product/" . $params['productId'];
        $header = [
            'cookie:' . $params['cookie'],
            'x-xsrftoken:' . $params['xToken'],
        ];
        $res = core_base::curl($url, null, 'get', $header);
        $data = [];
        if($html = strstr($res, 'window.__PRELOADED_STATE__ =')){
            $json = strstr($html, '</script>', true);
            $json = substr(trim($json), 29);
            $json = json_decode(trim($json), true);
            $data = $json['data']['product'][$params['productId']]['staticFields'];
        }
        return $data;
    }

    /**
     * 描述 : 搜索提示（关键词推荐）
     * 作者 : Zoro.Zhu
     */
    public static function getAutcomplete($params){
        $url = "https://www.wish.com/api/search/autcomplete";
        $header = [
            'cookie:' . $params['cookie'],
            'x-xsrftoken:' . $params['xToken'],
        ];
        $post = [
            'query' => $params['query'],
            'securedtouch_token' => $params['token'],
        ];
        $res = core_base::curl($url, $post, 'post', $header);
        $response = json_decode($res, true);
        if(!empty($response['msg'])) {
            return false;
        }
        if(!empty($response['data']['suggestions'])) {
            return $response['data']['suggestions'];
        }
        return [];
    }

    /**
     * 描述 : 获取产品情况
     * 作者 : Zoro.Zhu
     */
    public static function getProductStats($params){
        $url = "https://merchant.wish.com/api/stats/product/daily";
        $header = [
            'cookie:' . $params['cookie'],
            'x-xsrftoken:' . $params['xToken'],
        ];
        $post = [
            'start' => strtotime(date('Y-m-d', time() - 86400 * $params['day'])),
            'end' => strtotime(date('Y-m-d', time())),
            'merchant_id' => $params['merchantId'],
            'product_id' => $params['id'],
        ];
        $res = core_base::curl($url, $post, 'post', $header);
//        $res = of_base_com_net::request($url, ['post' => $post, 'header' => $header]);
        $response = json_decode($res, true);
        if(!empty($response['msg'])) {
            return false;
        }
        if(!empty($response['data']['series'])) {
            return $response['data']['series'];
        }
        return [];
    }

    /**
     * 描述 : 获取销售图表的数据
     * 作者 : Zoro.Zhu
     */
    public static function getFlow($params){
        $url = "https://merchant.wish.com/api/stats/merchant/daily";
        $header = [
            'cookie:' . $params['cookie'],
            'x-xsrftoken:' . $params['xToken'],
        ];
        $post = [
            'start' => strtotime(date('Y-m-d', time() - 86400 * $params['day'])),
            'end' => strtotime(date('Y-m-d', time())),
        ];
        $res = core_base::curl($url, $post, 'post', $header);
//        $res = of_base_com_net::request($url,['post' => $post,'header' => $header]);
        $response = json_decode($res, true);
        if(!empty($response['msg'])) {
            return false;
        }
        if(!empty($response['data']['series'])) {
            return $response['data']['series'];
        }
        return [];
    }

    /**
     * 描述 : 登录，获取session
     * 作者 : Zoro.Zhu
     */
    public static function login($params){
        $url = "https://merchant.wish.com/api/login";
        $header = [
            'Cookie: _xsrf=' . $params['xToken'] . '; bsid=' . $params['bsid'] . '; rd='  . $params['rd'] ,
            'X-XSRFToken: ' . $params['xToken'],
        ];
        $post = [
            'password' => $params['password'],
            'username' => $params['username'],
            'remember_me' => 'true',
        ];
        $res['header'] = core_base::posttoken($url, $post, $header);
//        $res = of_base_com_net::request($url, ['post' => $post, 'header' => $header]);
        $cookie = [];
        if(!empty($res['header'])){
            $responseHeader = explode("\n", $res['header']);
            foreach($responseHeader as $value){
                $value = trim($value);
                if(substr($value, 0, 12) == 'set-cookie: '){
                    $v = explode(';', substr($value, 12));
                    $cookie[] = $v[0];
                }
            }
        }
        return $cookie;
    }

    /**
     * 描述 : 获取店铺余额情况
     * 作者 : Zoro.Zhu
     */
    public static function getBalance($params){
        $url = "https://merchant.wish.com/api/get_account_balance";
        $header = [
            'Cookie:' . $params['cookie'],
            'X-XSRFToken:' . $params['xToken'],
        ];
        $res = core_base::curl($url, null, 'post', $header);
//        $res = of_base_com_net::request($url,['post' => [],'header' => $header]);
        $response = json_decode($res, true);
        if(!empty($response['msg'])) {
            return false;
        }
        if(!empty($response['data']['account_balances']['CNY'])) {
            return $response['data']['account_balances']['CNY'];
        }
        return [];
    }

    /**
     * 描述 : 当前余额列表
     * 作者 : Zoro.Zhu
     */
    public static function nowBalanceList($params){
        $url = "https://merchant.wish.com/api/line-item/get";
        $header = [
            'cookie:' . $params['cookie'],
            'x-xsrftoken:' . $params['xToken'],
        ];
        $post = [
            'confirmed' => 'true',
            'count' => 100,
            'currency' => 'CNY',
            'id_type' => 'id',
            'id_val' => '',
            'item_types' => '',
            'merchant_id' => $params['merchantId'],
            'start' => 0,
        ];
        $res = core_base::curl($url, $post, 'post', $header);
        $response = json_decode($res, true);
        if(!empty($response['msg'])) {
            return false;
        }
        if(!empty($response['data']['results'])) {
            return $response['data']['results'];
        }
        return [];
    }

    /**
     * 描述 : 批量获取关键词热度和竞争力
     * 作者 : Zoro.Zhu
     */
    public static function getKeyword($params){
        $url = "https://merchant.wish.com/api/product-boost/keyword/multi-get";
        $header = [
            'cookie:' . $params['cookie'],
            'x-xsrftoken:' . $params['xToken'],
        ];
        $post = [
            'keywords' => $params['keywords'],
        ];
        $res = core_base::curl($url, $post, 'post', $header);
        $response = json_decode($res, true);
        if(!empty($response['msg'])) {
            return false;
        }
        if(!empty($response['data'])) {
            return $response['data'];
        }
        return [];
    }

}

return true;